package com.car.service;

import com.car.entity.*;
import com.car.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CellTypeMapper cellTypeMapper;

    @Autowired
    private CarInfoMapper carInfoMapper;

    @Autowired
    private PowerSystemMapper powerSystemMapper;

    @Autowired
    private ChassisBrakeMapper chassisBrakeMapper;

    @Autowired
    private SecurityConfigurationMapper securityConfigurationMapper;

    @Autowired
    private DrivigAssistanceMapper drivigAssistanceMapper;

    @Autowired
    private ExternalConfigMapper externalConfigMapper;

    @Autowired
    private SeatConfigurationMapper seatConfigurationMapper;

    @Autowired
    private CellPackMapper cellPackMapper;

    @Autowired
    private BasicInfoMapper basicInfoMapper;

    @Autowired
    private SizeInfoMapper sizeInfoMapper;

    @Autowired
    private CarBatteryMapper carBatteryMapper;

    public void cellTypeCache(){
        List<CellType> types = cellTypeMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("CellType");
        if(types!=null){
            for (CellType cellType:types){
                hash.put("CellType",cellType.getId(),cellType);
            }
        }
    }

//    public void carInfoCache(){
//        List<CarInfo> carInfos = carInfoMapper.selectByExample(null);
//        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
//        redisTemplate.delete("CarInfo");
//        if(carInfos!=null){
//            for (CarInfo carInfo:carInfos){
//                hash.put("CarInfo",carInfo.getId(),carInfo);
//            }
//        }
//    }

    public void powerSystemCache(){
        List<PowerSystem> powerSystems = powerSystemMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("PowerSystem");
        if(powerSystems!=null){
            for (PowerSystem powerSystem:powerSystems){
                hash.put("PowerSystem",powerSystem.getId(),powerSystem);
            }
        }
    }

    public void chassisBrakeCache(){
        List<ChassisBrake> chassisBrakes = chassisBrakeMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("ChassisBrake");
        if(chassisBrakes!=null){
            for (ChassisBrake chassisBrake:chassisBrakes){
                hash.put("ChassisBrake",chassisBrake.getId(),chassisBrake);
            }
        }
    }

    public void securityConfigurationCache(){
        List<SecurityConfiguration> securityConfigurations = securityConfigurationMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("SecurityConfiguration");
        if(securityConfigurations!=null){
            for (SecurityConfiguration securityConfiguration:securityConfigurations){
                hash.put("SecurityConfiguration",securityConfiguration.getId(),securityConfiguration);
            }
        }
    }

    public void drivigAssistanceCache(){
        List<DrivigAssistance> drivigAssistances = drivigAssistanceMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("DrivigAssistance");
        if(drivigAssistances!=null){
            for (DrivigAssistance drivigAssistance:drivigAssistances){
                hash.put("DrivigAssistance",drivigAssistance.getId(),drivigAssistance);
            }
        }
    }

    public void externalConfigCache(){
        List<ExternalConfig> externalConfigs = externalConfigMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("ExternalConfig");
        if(externalConfigs!=null){
            for (ExternalConfig externalConfig:externalConfigs){
                hash.put("ExternalConfig",externalConfig.getId(),externalConfig);
            }
        }
    }

    public void seatConfigurationCache(){
        List<SeatConfiguration> seatConfigurations = seatConfigurationMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("SeatConfiguration");
        if(seatConfigurations!=null){
            for (SeatConfiguration seatConfiguration:seatConfigurations){
                hash.put("SeatConfiguration",seatConfiguration.getId(),seatConfiguration);
            }
        }
    }

    public void cellPackCache(){
        List<CellPack> cellPacks = cellPackMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("CellPack");
        if(cellPacks!=null){
            for (CellPack cellPack:cellPacks){
                hash.put("CellPack",cellPack.getId(),cellPack);
            }
        }
    }

    public void basicInfoCache(){
        List<BasicInfo> basicInfos = basicInfoMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("BasicInfo");
        if(basicInfos!=null){
            for (BasicInfo basicInfo:basicInfos){
                hash.put("BasicInfo",basicInfo.getId(),basicInfo);
            }
        }
    }

    public void sizeInfoCache(){
        List<SizeInfo> sizeInfos = sizeInfoMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("SizeInfo");
        if(sizeInfos!=null){
            for (SizeInfo sizeInfo:sizeInfos){
                hash.put("SizeInfo",sizeInfo.getId(),sizeInfo);
            }
        }
    }

    public void carBatteryCache(){
        List<CarBattery> carBatteries = carBatteryMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("CarBattery");
        if(carBatteries!=null){
            for (CarBattery carBattery:carBatteries){
                hash.put("CarBattery",carBattery.getId(),carBatteries);
            }
        }
    }
    @Autowired
    private InteriorConfigMapper interiorConfigMapper;
    public void interiorCache(){
        List<InteriorConfig> interiorConfigs = interiorConfigMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("InteriorConfig");
        if(interiorConfigs!=null){
            for (InteriorConfig interiorConfig:interiorConfigs){
                hash.put("InteriorConfig",interiorConfig.getId(),interiorConfig);
            }
        }
    }
    @Autowired
    private InformationEntertainmentMapper informationEntertainmentMapper;
    public void inforMAtionCache(){
        List<InformationEntertainment> informationEntertainments = informationEntertainmentMapper.selectByExample(null);
        HashOperations<Object, Object, Object> hash = redisTemplate.opsForHash();
        redisTemplate.delete("InformationEntertainment");
        if(informationEntertainments!=null){
            for (InformationEntertainment informationEntertainment:informationEntertainments){
                hash.put("InformationEntertainment",informationEntertainment.getId(),informationEntertainments);
            }
        }
    }

}
